#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <list>
#include <time.h>
#include "charsetconvert.h"
#include "senwordpro.h"

#include "re2/re2.h"

using namespace std;

#define CSTTIMES 100000

void usage()
{
	printf( "./senmain  senworddictfile   contentfile\n" );
}

void WriteFile( const char * filename, const char* buf, int iSize);

int main(int argc, char ** argv)
{
	if( argc < 3 )
	{
		usage();
		return -1;
	}

	CSenWordPro csp;
	CCharSetConvert cs;
	struct stat st;
	char * szRetSenWord = NULL;
	int fd;
	char * o_buf = NULL;
	char * s = NULL;
	int iRes = 0;
	int iCircleCounts = CSTTIMES;
	time_t curTime = 0;

	csp.init( argv[1] );

    /*RE2::Options opt;
    opt.set_max_mem(32<<20);

    string strRegexp;
    if( csp.getRegexp(strRegexp)==0 )
    {
        WriteFile( argv[1], strRegexp.c_str(), strRegexp.length() );
    }
    RE2 pattern(strRegexp, opt);

    if( csp.getRegexpEmbed(strRegexp)==0 )
    {
        WriteFile( "embed", strRegexp.c_str(), strRegexp.length() );
    }
    RE2 patternEmbed(strRegexp, opt);
    //return 0;


    RE2::GlobalReplace( &strUser, pattern, "***" );
    RE2::GlobalReplace( &strUser, patternEmbed, "***" );
*/
    string strUser = string(argv[2]);
    char * szUser = (char*)malloc( strUser.length()+1 );
    memset( szUser, 0, strUser.length() );
    snprintf( szUser, strUser.length(), "%s", strUser.c_str() );
    //memcpy( szUser, strUser.c_str(), strUser.length() );
    //csp.replaceSenWord( szUser );
    iRes = csp.queryNickName( strUser.c_str() );
    
    if( iRes>0 )
    {
        printf( "role name = %s\n", iRes==2?"special char":"sensitive word" );
    }else
    {
        printf( "not sensitive word\n" );
    }
    //cout<< szUser << endl;;
    free( szUser );

    sleep( 1000000 );
/*
	if((fd = open(argv[2], O_RDONLY, 0644)) > 0)
	{
		if(fstat(fd, &st) == 0 && st.st_size > 0 && (s = (char *)calloc(1, st.st_size + 1)))
		{
			unsigned int o_size = st.st_size*3 + 1;
			o_buf = (char*)malloc(o_size);
			
			if(read(fd, s, st.st_size) > 0
				&& (iRes=cs.CharsetConvert(s, st.st_size, o_buf, o_size  )) > 0)
			{
				curTime =  time(NULL);
				while( iCircleCounts-->0 )	
				{
					if( 0==csp.querySenWord( (const char*)o_buf, &szRetSenWord ) )
					{
						printf( "query senword:[%s]\n", szRetSenWord );
					}
					else
					{
						//printf( "not found.\n" );
					}
				}
				printf( "compute %d times,consume time:%u.\n", CSTTIMES, time(NULL)-curTime );
			}
			free(s);
			s = NULL;
			free(o_buf);
			o_buf = NULL;
		}
		close(fd);
	}
	else
	{
		printf( "open content fail.\n" );
		return -1;
	}
*/

/*
      SenWordTrie t;
      t.insert("aa"); 
      t.insert("abacus");
      t.insert("abalone"); 
      t.insert("abandon");
      t.insert("abandoned"); 
      t.insert("abashed");
      t.insert("abate");
      t.insert("VERB"); 
      t.insert("this");
      t.insert("PRON");

      char * entry = NULL;

	printf( "----------------------start\n" );
      t.dump();
	printf( "----------------------end\n" );

    if (0==t.querySenWord("this", &entry))
        cout<<"'this' was found. pos: "<<entry<<endl;
    if (0==t.querySenWord("abate", &entry))
        cout<<"'abate' is found. pos: "<<entry<<endl;
    if (0==t.querySenWord("baby", &entry))
        cout<<"'baby' is found. pos: "<<entry<<endl;
    else
        cout<<"'baby' does not exist at all!"<<endl;
    
    if (0==t.querySenWord("aa", &entry))
        cout<<"'aa was found. pos: "<<entry<<endl;

    if (0==t.querySenWord("aven", &entry))
        cout<<"'aven' is found. pos: "<<entry<<endl;
    else
        cout<<"'aven' does not exist at all!"<<endl;

    if (0==t.querySenWord("VERBfjlsaoe", &entry))
        cout<<"'versbksjflk was found. pos: "<<entry<<endl;
*/

	return 0;
}
 
void WriteFile( const char * filename, const char* buf, int iSize)
{
    int fd = 0;
    char dstFileName[256] = {0};
    sprintf( dstFileName, "%s-exp-UTF-8", filename );
    printf( "gen filename:%s\n", dstFileName );
    if((fd = open(dstFileName, O_RDWR|O_CREAT, 0644)) > 0)
    {
        int iRet = write(fd, buf, iSize);
        if( iRet>=0 )
        {
            printf("write file ok [%d].\n", iRet);
        }
        else
        {
            printf("write file fail.[%s]\n", strerror(errno) );
        }
        close(fd);
    }
    else{
        printf("open file fail.[%s]\n", strerror(errno) );
        return;
    }
}
